/**
 * Created by wang on 16-7-1.
 */
$(document).ready(function () {
    $("#tofriend_btn").on("click", function () {
        judge($(this));
    });


    $("#tolover_btn").on("click", function () {
        judge($(this));
    });


});

var words = 5;
$("input[name='lover_poem']").change(function () {
    var poem_type = $("input:radio[name='lover_poem']:checked").next().text();
    if (poem_type == "五言") {
        words = 5;
    } else {
        words = 7;
    }
})
$("input:radio[name='friend-poem']").change(function () {
    var poem_type = $("input[name='friend-poem']:checked").next().text();
    if (poem_type == "五言") {
        words = 5;
    } else {
        words = 7;
    }
})

function judge(zj) {
    var sents;
    var heads;
    console.log("inputcontent:" + $("#inputcontent").val())
    if ($("#inputcontent").val().replace(/[ ]/g, "").length < 2 || $("#inputcontent").val().replace(/[ ]/g, "").length > 8) {
        $.alert("请输入2-8字的尊称");
        return;
    }

    if ($("#inputcontent").val().replace(/[ ]/g, "").length <= 4) {
        sents = 4;
    } else {
        sents = 8;
    }
    $(".index-wrapper").css("opacity", 0);
    $.showLoading("鸭鸭在挥墨泼亳中...");
    heads = $("#inputcontent").val().replace(/[ ]/g, "");
    var data = {
        'words': words,
        'sents': sents,
        'title': zj.attr('value'),
        'heads': heads,
        'times': 1
    };
    var post_data = JSON.stringify(data);
    $.ajax({
        url: 'http://kcloudtest.iflytek.com/zb/poem?data=' + post_data,
        // url: 'http://127.0.0.1:10063/zb/poem?data=' + post_data,
        type: 'get',
        success: function (data) {
            get_data = JSON.parse(data);
            var poem_con = get_data.result_text;
            window.location.href = "poem.html?poem_type=" + words + "&poem_sens=" + sents + "&poem_con=" + poem_con + "&poem_title=" + zj.attr('value') + "&poem_heads=" + heads;
            $.hideLoading();
        }, error: function (data) {
            $.hideLoading();
            $.alert("人气太旺,请稍后...");
            $.alert("鸭鸭词穷了~~换个词吧~");
        }
    })
}

